package com.yf.esms.controller;

import com.yf.esms.common.ResponseCode;
import com.yf.esms.common.ResultComponent;
import com.yf.esms.model.vo.*;
import com.yf.esms.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @author 99698
 */
@Api(tags = "商品搜索")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/create")
    @ApiOperation(value = "创建商品索引")
    public ResultComponent<String> createIndex(@RequestParam("indexName") String indexName) throws IOException {
        Boolean index = goodsService.createIndex(indexName);
        if (index){
            return new ResultComponent<>(true,
                    ResponseCode.SUCCESS.getMsg(),
                    ResponseCode.SUCCESS.getCode(),
                    indexName);
        }else {
            return new ResultComponent<>(true,
                    ResponseCode.FAILED.getMsg(),
                    ResponseCode.FAILED.getCode(),
                    "创建索引"+indexName+"失败！");
        }
    }

    @PostMapping("/insertOne")
    @ApiOperation(value = "添加单条商品信息")
    public ResultComponent<String> insertOne(@RequestBody GoodsInsertOneVo goodsInsertOneVo) throws IOException {
        String id = goodsService.insertOne(goodsInsertOneVo);
        return new ResultComponent<>(true,ResponseCode.SUCCESS.getMsg()+", 添加的该文档的id是："+id,ResponseCode.SUCCESS.getCode(),id);
    }

    @PostMapping("/insertBatch")
    @ApiOperation(value = "批量添加商品信息")
    public ResultComponent<List<String>> insertBatch(@RequestBody GoodsInsertBatchVo goodsInsertBatchVo) throws IOException {
        List<String> list = goodsService.insertBatch(goodsInsertBatchVo);
        return new ResultComponent<>(true,ResponseCode.SUCCESS.getMsg()+",添加的文档的id有："+list,ResponseCode.SUCCESS.getCode(),list);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询索引中全部商品")
    public ResultComponent<SearchGoodsVo> all(@RequestParam("indexName") String indexName) throws IOException {
        SearchGoodsVo searchGoodsVo = goodsService.all(indexName);
        return new ResultComponent<>(true,ResponseCode.SUCCESS.getMsg(),ResponseCode.SUCCESS.getCode(), searchGoodsVo);
    }

    @PostMapping("/searchByKeyword")
    @ApiOperation(value = "搜索商品")
    public ResultComponent<SearchGoodsVo> searchByKeyword(@RequestBody GoodsSearchByKeywordVo goodsSearchByKeywordVo) throws IOException {
        SearchGoodsVo searchGoodsVo = goodsService.searchByKeyword(goodsSearchByKeywordVo);
        return new ResultComponent<>(true,ResponseCode.SUCCESS.getMsg(),ResponseCode.SUCCESS.getCode(),searchGoodsVo);
    }

    @PostMapping("/selectByCatId")
    @ApiOperation(value = "根据分类id搜索id")
    public ResultComponent<SearchGoodsVo> selectByCatId(@RequestBody GoodsSearchByCatIdVo goodsSearchByCatIdVo) throws IOException {
        SearchGoodsVo searchGoodsVo = goodsService.selectByCatId(goodsSearchByCatIdVo);
        return new ResultComponent<>(true,ResponseCode.SUCCESS.getMsg(),ResponseCode.SUCCESS.getCode(),searchGoodsVo);
    }
}
